﻿using FluentValidation;
using Mes.Application.Common;
using Mes.Application.DTO.Inputs.Quality;
using Mes.Application.DTO.Results.Quality;
using Mes.Application.Services.Quality;
using Mes.Domain.BoundedContext;
using Microsoft.AspNetCore.Mvc;

namespace Mes.Application.Web.Controllers.Quality
{
    [ApiController]
    [Route("/api/v1")]
    public class SamplePlanController : ControllerBase
    {
        private readonly ISamplePlanService _samplePlanService;
        private readonly IValidator<SamplePlanAddingInput> _samplePlanAddingValidator;
        private readonly IValidator<SamplePlanUpdatingInput> _samplePlanUpdatingValidator;
        private readonly IValidator<GettingSamplePlanPagedInput> _getSamplePlanPagedValidator;
        private readonly ILogger<SamplePlanController> _logger;

        public SamplePlanController(ISamplePlanService samplePlanService, IValidator<SamplePlanAddingInput> samplePlanAddingValidator, IValidator<SamplePlanUpdatingInput> samplePlanUpdatingValidator,
            IValidator<GettingSamplePlanPagedInput> getSamplePlanPagedValidator, ILogger<SamplePlanController> logger)
        {
            _samplePlanService = samplePlanService;
            _samplePlanAddingValidator = samplePlanAddingValidator;
            _samplePlanUpdatingValidator = samplePlanUpdatingValidator;
            _getSamplePlanPagedValidator = getSamplePlanPagedValidator;
            _logger = logger;
        }

        /// <summary>
        /// 添加采样计划
        /// </summary>
        /// <param name="samplePlanInput"></param>
        /// <returns></returns>
        [HttpPost("sampleplan")]
        public async Task<ActionResult> AddSamplePlanAsync([FromBody] SamplePlanAddingInput samplePlanInput)
        {
            try
            {
                var input = await _samplePlanAddingValidator.ValidateAsync(samplePlanInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _samplePlanService.AddSamplePlanAsync(samplePlanInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 编辑采样计划
        /// </summary>
        /// <param name="samplePlanInput"></param>
        /// <returns></returns>
        [HttpPut("sampleplan")]
        public async Task<ActionResult> UpdateSamplePlanAsync([FromBody] SamplePlanUpdatingInput samplePlanInput)
        {
            try
            {
                var input = await _samplePlanUpdatingValidator.ValidateAsync(samplePlanInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _samplePlanService.UpdateSamplePlanAsync(samplePlanInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除采样计划
        /// </summary>
        /// <param name="samplePlanIds"></param>
        /// <returns></returns>
        [HttpDelete("sampleplan")]
        public async Task<ActionResult> DeleteSamplePlanAsync(int[] samplePlanIds)
        {
            try
            {
                await _samplePlanService.DeleteSamplePlanAsync(samplePlanIds);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 通过采样计划Id查询采样计划和其关联的采样计划频率
        /// </summary>
        /// <param name="samplePlanId"></param>
        /// <returns></returns>
        [HttpGet("sampleplan/id")]
        public async Task<ActionResult<SamplePlanResult>> GetSamplePlanByIdAsync([FromQuery] int samplePlanId)
        {
            try
            {
                var samplePlan = await _samplePlanService.GetSamplePlanByIdAsync(samplePlanId);
                return Ok(samplePlan);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 分页查询采样计划和其关联的采样计划频率
        /// </summary>
        /// <param name="samplePlanPagedinput"></param>
        /// <returns></returns>
        [HttpGet("sampleplans")]
        public async Task<ActionResult<PagedItemsData<SamplePlanResult>>> GetSamplePlansAsync([FromQuery] GettingSamplePlanPagedInput samplePlanPagedinput)
        {
            try
            {
                var input = await _getSamplePlanPagedValidator.ValidateAsync(samplePlanPagedinput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }
                
                var samplePlans = await _samplePlanService.GetSamplePlansAsync(samplePlanPagedinput);
                return Ok(samplePlans);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }
    }
}
